package com.gjs.management.controller;

import cn.hutool.core.bean.BeanUtil;
import com.gjs.common.enums.entity.ConfigTypeEnum;
import com.gjs.common.manager.response.RspBody;
import com.gjs.management.pojo.dto.agreement.AgreementDTO;
import com.gjs.management.pojo.dto.config.ConfigDTO;
import com.gjs.management.service.IAgreementService;
import com.gjs.management.service.IConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@Api(tags = "系统配置接口")
@RequiredArgsConstructor
@RequestMapping("/config")
public class ConfigController {

    private final IAgreementService agreementService;
    private final IConfigService configService;

    @GetMapping("/agreement/agreement_selector")
    @ApiOperation("协议配置-协议map")
    public RspBody<List<AgreementDTO>> agreementSelector(){
        List<AgreementDTO> agreementDTOList = agreementService.list()
                .stream()
                .map(row -> BeanUtil.copyProperties(row,AgreementDTO.class))
                .collect(Collectors.toList());
        return RspBody.success(agreementDTOList);
    }

    @GetMapping("/agreement/list")
    @ApiOperation("协议配置-列表")
    public RspBody<List<ConfigDTO>> listConfig(){
        return RspBody.success(configService.list(ConfigTypeEnum.AGREEMENT));
    }

    @PostMapping("/agreement/save")
    @ApiOperation("协议配置-保存")
    public RspBody<String> saveConfig(@RequestBody List<ConfigDTO> configDTOList){
        configService.save(configDTOList,ConfigTypeEnum.AGREEMENT);
        return RspBody.success("成功");
    }
}
